package com.west_traveler.find_page_fragment;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.west_traveler.application.BaseApplication;
import com.west_traveler.entity.entity_ranking_page.RankingData;
import com.west_traveler.entity.entity_ranking_page.RankingFirstData;
import com.west_traveler.entity.entity_ranking_page.RankingHeadList;
import com.west_traveler.recyclerview_adapter.RankingFragmentRecyclerViewAdapter;
import com.west_traveler.ximalayafm.R;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
*
* Description:发现页 榜单界面
* @author Wang_JunJie
* @date 2016/12/5
*
*/

public class RankingFragment extends Fragment implements Callback<RankingFirstData> {


    private RecyclerView recyclerRanking;

    public static RankingFragment getInstance() {
        return new RankingFragment();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_ranking, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //找到控件
        recyclerRanking = ((RecyclerView) view.findViewById(R.id.recycler_frg_ranking_id));
        //数据源（下载）
        ((BaseApplication) getActivity().getApplication())
               .getService()
                .getRankingFirstData()
                .enqueue(this);
    }

    @Override
    public void onResponse(Call<RankingFirstData> call, Response<RankingFirstData> response) {
        //数据源
        //榜单信息数据源
        List<RankingData> datas = response.body().getDatas();
        //头部广告
        RankingHeadList rankingHead = response.body().getFocusImages().getList().get(0);
        Log.i("调试",rankingHead.getUrl());
        //适配器
        RankingFragmentRecyclerViewAdapter adapter = new RankingFragmentRecyclerViewAdapter(getContext(), datas, rankingHead);
//        //绑定适配器
        recyclerRanking.setAdapter(adapter);
        //设置item之间的距离
        recyclerRanking.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                if (parent.getChildAdapterPosition(view)!=0) {
                    outRect.bottom = 30;
                }
            }
        });

    }

    @Override
    public void onFailure(Call<RankingFirstData> call, Throwable t) {

    }
}
